package com.yjhb.android.sbz.dialog;

import android.app.Activity;
import android.content.Context;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.view.nine.NineGridImageView;
import com.jojo.android.zxlib.view.nine.NineGridImageViewAdapter;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/25 15:58
 */
public class ImageTextDialog extends BaseDialog {
    private final String TAG = "ImageTextDialog";
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.close_iv)
    ImageView closeIv;
    @BindView(R.id.input_et)
    EditText inputEt;
    @BindView(R.id.addImage_iv)
    ImageView addImageIv;
    @BindView(R.id.nineGridImageView)
    NineGridImageView<LocalMedia> nineGridImageView;
    @BindView(R.id.submit_tv)
    TextView submitTv;

    List<LocalMedia> medias = new ArrayList<>();
    NineGridImageViewAdapter adapter;
    public ImageTextDialog(@NonNull Fragment context) {
        super(context);
    }

    public ImageTextDialog(Activity context) {
        super(context);
    }

    @Override
    protected void init() {
        nineGridImageView.setAdapter(adapter = new NineGridImageViewAdapter<LocalMedia>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, LocalMedia o) {
                Glide.with(context).load(o.getPath())
                        .apply(new RequestOptions().placeholder(com.jojo.android.zxlib.R.drawable.xui_ic_default_img))
                        .into(imageView);
            }
        });
        nineGridImageView.setMaxSize(9);
        nineGridImageView.setShowStyle(0);
    }

    @OnClick(R.id.addImage_iv)
    void addImage() {
        Tool.getPictureSelector(activity, 9).
                selectionMedia(medias)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_image_text;
    }

    public EditText getInputView() {
        return inputEt;
    }

    @OnClick(R.id.submit_tv)
    void onOk() {
        if (onClickListener != null) {
            onClickListener.onClick(this,BUTTON_SUBMIT);
        }
    }

    @OnClick(R.id.close_iv)
    void close() {
        if (onClickListener != null) {
            onClickListener.onClick(this,BUTTON_CANCEL);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(EventMsg msg) {
        if (msg.msg == EventMsg.IMAGE_SELECTED_SUCCESS) {
            medias.clear();
            if (msg != null)
                medias.addAll((List<LocalMedia>) msg.object);
            refresh();
        }
    }

    private void refresh() {
        nineGridImageView.setImagesData(new ArrayList<>(medias));
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    public String getContent() {
        return Tool.getText(inputEt);
    }

    public List<LocalMedia> getImgs() {
        return medias;
    }

    public void setTitle(CharSequence text) {
        titleTv.setText(text);
    }

}
